<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Request;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\AdminZitjxx as M;
use app\common\model\AdminZavote as MA;
use app\common\model\AdminZbuserinfo as MB;
use app\common\model\AdminZccandidate as MC;
use app\common\model\AdminZegzhpz as ME;
use think\facade\Db;

class AdminZitjxx extends Model
{
    use SoftDelete;
     protected $deleteTime = false;

    // 记录报名
    public static function updatabaoming($biaoid,$utoken,$data)
    {
        $res = MA::find($biaoid);
        if($res['baoming'] == 1){
            $data = explode(';',$data);
            $data = array_filter($data);
            $dataarr = array();
            foreach ($data as $k => $v) {
                $dataarr[$k] = explode('=',$v);
            }
            $dataarrer = array();
            foreach ($dataarr as $k => $v) {
                $dataarrer[$v[0]] = $v[1];
            }
            // $d=['code'=>102,'message'=>$dataarr,'message1'=>$dataarrer];
            // return ['code'=>200,'message'=>'成功','data'=>$d];
            
            $da['biaoid']=$biaoid;
            
            if(isset($dataarrer['mingcheng'])){
                $da['name']=$dataarrer['mingcheng'];
            }else{
                $da['name']='';
            }
            
            $maxbianhao = MC::where('biaoid', $biaoid)->max('bianhao');
            $da['bianhao']=$maxbianhao+1;
            
            if(isset($dataarrer['tel'])){
                $da['tel']=$dataarrer['tel'];
            }else{
                $da['tel']='';
            }
            if(isset($dataarrer['outurl'])){
                $da['outurl']=$dataarrer['outurl'];
            }else{
                $da['outurl']='';
            }
            if(isset($dataarrer['yinpin'])){
                $da['mp3url']=$dataarrer['yinpin'];
            }else{
                $da['mp3url']='';
            }
            if(isset($dataarrer['shipin'])){
                $da['video']=$dataarrer['shipin'];
            }else{
                $da['video']='';
            }
            if(isset($dataarrer['group'])){
                $da['group']=$dataarrer['group'];
            }else{
                $w['utoken']=$utoken;
                $w['shuxing']='组别';
                $shuxingzhi = Db::name('admin_zitjxx')->where($w)->find();
                if($shuxingzhi){
                    $da['group']=$shuxingzhi['shuxingzhi'];
                }else{
                    $da['group']='';
                }
            }
            if(isset($dataarrer['jianjie'])){
                $da['content']=$dataarrer['jianjie'];
            }else{
                $da['content']='';
            }
            if(isset($dataarrer['xiangqing'])){
                $da['xiangqing']=$dataarrer['xiangqing'];
            }else{
                $da['xiangqing']='';
            }
            if(isset($dataarrer['fengmiantu'])){
                $da['image']=$dataarrer['fengmiantu'];
            }else{
                $da['image']='';
            }
            if(isset($dataarrer['xingming'])){
                $da['xingming']=$dataarrer['xingming'];
            }else{
                $da['xingming']='';
            }
            if(isset($dataarrer['zubie'])){
                $da['zubie']=$dataarrer['zubie'];
            }else{
                $da['zubie']='';
            }
            if($res['bmshenhe'] == 0){
                $da['shenhe']=1;
            }elseif($res['bmshenhe'] == 1){
                $da['shenhe']=0;
            }
            $da['datetime'] = date("Y-m-d H:i:s",time());
            $da['create_time'] = date("Y-m-d H:i:s",time());
            $da['update_time'] = date("Y-m-d H:i:s",time());
            $da['uid']=$utoken;
            
            // $whe['uid']=$utoken;
            // $whe['biaoid']=$biaoid;
            // $timetoday = date("Y-m-d",time());   //今日00时间戳
            // $time2today = date('Y-m-d',strtotime('+1 day'));  //  今日结束时间 23:59戳
            // $sfbmres = Db::name("admin_zccandidate")->where($whe)->where([
            //     ['datetime','>',$timetoday],
            //     ['datetime','<',$time2today]
            // ])->count();
            // $sfbmres = Db::name("admin_zccandidate")->where($whe)->count();
            // if($sfbmres == $res['bmnum']){
            //     $d=['code'=>105,'message'=>'报名次数已用完'];
            //     return ['code'=>200,'message'=>'成功','data'=>$d];
            // }
            
            $res=Db::name("admin_zccandidate")->insertGetId($da);
            if($res){
                $reshx = MC::find($res);
                $reszhx = MC::where('biaoid', $reshx['biaoid'])->where('shenhe',0)->count();
                MA::where('id', $biaoid)->update(['zonghouxuan' => $reszhx]);
                $d=['code'=>102,'message'=>'提交信息成功'];
                return ['code'=>200,'message'=>'成功','data'=>$d];
            }else{
                $d=['code'=>104,'message'=>'提交信息失败'];
                return ['code'=>200,'message'=>'成功','data'=>$d];
            }
        }else{
            $d=['code'=>103,'message'=>'无'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }
    }

    // 记录属性
    public static function updatashuxing($biaoid,$utoken,$data)
    {
        $w['biaoid']=$biaoid;
        $w['utoken']=$utoken;
        $res = self::order('id','desc')->where($w)->find();
        if($res){
            $d=['code'=>101,'message'=>'已提交过信息'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }
        $res = MA::find($biaoid);
        if($res['shuxing'] == 1){
            $shuxing = explode(',',$res['shuxingzhi']);
            $shuxing = array_filter($shuxing);
            
            $shuxingxq = array();
            foreach ($shuxing as $k => $v) {
                $shuxingzhi = ME::find($v);
                $shuxingxq[$k]['name'] = $shuxingzhi['wxgzgname'];
                $shuxingxq[$k]['ziduanming'] = $shuxingzhi['appsecret'];
            }
            // $shuxingxqimg = array();
            // if($res['bmnumimg'] != 0){
            //     $shuxingxqimg['name'] = "上传图片";
            //     $shuxingxqimg['ziduanming'] = "shuxingxqimg";
            //     array_push($shuxingxq,$shuxingxqimg);
            // }


            $data = explode(';',$data);
            $data = array_filter($data);
            $dataarr = array();
            foreach ($data as $k => $v) {
                $dataarr[$k] = explode('=',$v);
            }


            foreach ($shuxingxq as $k => $v) {
                foreach ($dataarr as $kk => $vv) {
                    if($v['ziduanming'] == $vv[0]){
                        $shuxingxq[$k]['zhi'] = $vv[1];
                    }
                }
            }

            foreach ($shuxingxq as $k => $v) {
                $da['biaoid'] = $biaoid;
                $da['utoken'] = $utoken;
                $da['shuxing'] = $v['name'];
                $da['ziduanming'] = $v['ziduanming'];
                $da['shuxingzhi'] = $v['zhi'];
                $da['ip'] =  getip();
                $da['datetime'] =  time();
                self::create($da);
            }


            $d=['code'=>102,'message'=>'提交信息成功'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }else{
            $d=['code'=>103,'message'=>'无'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }
    }

    // 前台文件删除
    public static function uploadqtimgdel($path)
    {
        $w['href']=$path;
        $res = Db::name('admin_photo')->where($w)->delete();
        if($res){
            $image_file = "../public/{$path}";//服务器文件存储路径
            if(file_exists($image_file)){
                // 删除文件
                unlink($image_file);
            }
            $d=['code'=>101,'message'=>'已删除'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }else{
            $d=['code'=>102,'message'=>'文件不存在'];
            return ['code'=>200,'message'=>'成功','data'=>$d];
        }
    }

}
